package com.cskaoyan.config;

import com.cskaoyan.realm.AdminRealm;
import com.cskaoyan.realm.WxRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //认证失败后重定向的url
        shiroFilterFactoryBean.setLoginUrl("/unAuthc");
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/admin/auth/login","anon");
        filterMap.put("/wx/auth/login", "anon");
        filterMap.put("/admin/auth/info","anon");
        filterMap.put("/wx/auth/regCaptcha", "anon");
        filterMap.put("/wx/auth/register", "anon");
        filterMap.put("/wx/auth/reset", "anon");
        filterMap.put("/wx/cart/index", "anon");
        filterMap.put("/wx/user/index", "anon");
        filterMap.put("/wx/home/index", "anon");
        filterMap.put("/wx/goods/count", "anon");
        filterMap.put("/wx/catalog/index", "anon");
        filterMap.put("/wx/search/index", "anon");
        filterMap.put("/wx/search/list", "anon");
        filterMap.put("/wx/goods/list", "anon");

        filterMap.put("/wx/catalog/current", "anon");
        filterMap.put("/wx/goods/category", "anon");
        filterMap.put("/wx/goods/detail", "anon");
        filterMap.put("/wx/goods/related", "anon");

        filterMap.put("/wx/search/helper", "anon");
        filterMap.put("/wx/cart/goodscount", "anon");
        filterMap.put("/mall28/storage/*","anon");

        filterMap.put("/unAuthc","anon");
        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    //配置SecurityManager
    public DefaultWebSecurityManager defaultWebSecurityManager(AdminRealm adminRealm,DefaultWebSessionManager defaultWebSessionManager,CustomAuthenticator authenticator){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(adminRealm);
        defaultWebSecurityManager.setSessionManager(defaultWebSessionManager);
        defaultWebSecurityManager.setAuthenticator(authenticator);
        return defaultWebSecurityManager;
    }

    @Bean
    //声明式鉴权
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public DefaultWebSessionManager defaultWebSessionManager(){
        return new AdminSessionManager();
    }

    @Bean
    public CustomAuthenticator authenticator(AdminRealm adminRealm, WxRealm wxRealm){
        CustomAuthenticator customAuthenticator = new CustomAuthenticator();
        ArrayList<Realm> realms = new ArrayList<>();
        realms.add(adminRealm);
        realms.add(wxRealm);
        customAuthenticator.setRealms(realms);
        return customAuthenticator;
    }
}
